/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-08-05 15:06:55
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-06 15:34:14
 * @FilePath: /110/59/http/Main.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "TcpServer.hpp"
#include "Http.hpp"

// 输入格式不对
void Usage(std::string proc)
{
    std::cout << "Usage: \n\t" << proc << "local_port\n"
              << std::endl;
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    LOG(DEBUG, "=========================\n");

    std::string userdata; // 用户数据
    if (req->Method() == "GET")
    {
        userdata = req->Args(); // 用户数据在_args中
    }
    else if (req->Method() == "POST")
    {
        userdata = req->Text(); // 用户数据在_text中
    }
    else
    {
    }

    // 处理数据
    // 这里没处理

    LOG(DEBUG, "enter data handler,data is : %s\n", userdata.c_str()); // 打印用户数据

    auto response = Factory::BuildHttpResponse(); // 创建响应对象
    response->AddStatusLine(200, "OK");
    response->AddHeader("Content-Type", "text/html");
    response->AddText("<html><h1>handler data done</h1></html>"); // 添加响应内容
    LOG(DEBUG, "=========================\n");
    return response;
}

// ./httpserver port
int main(int arg, char *argv[])
{

    if (arg != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    uint16_t port = std::stoi(argv[1]);

    HttpServer httpservice;
    httpservice.AddHandler("/login", Login);
    TcpServer tcpsvr(port, std::bind(&HttpServer::HandlerHttpReqest, &httpservice, std::placeholders::_1)); // 需要httpserver的东西才能执行函数使用传对象 在执行任务时调用HttpServer类中的HandlerHttpReqest函数
    tcpsvr.Loop();

    return 0;
}